<?php
class Classes_Image {
	var $path;
	var $destination;
	var $maxWidth = 500;
	var $maxHeight = 500;
	var $quality = 90;
	
	function __construct($path,$destination,$maxWidth,$maxHeight,$quality){		
		if($path) { $this->path = $path; $this->destination = $path;}
		if($destination) $this->destination = $destination;
		if($maxWidth) $this->maxWidth = $maxWidth;
		if($maxHeight) $this->maxHeight = $maxHeight;
		if($quality) $this->quality = $quality;
	}
	
	function resize($path,$destination,$maxWidth,$maxHeight,$quality){	
		if($path) { $this->path = $path; $this->destination = $path;}
		if($destination) $this->destination = $destination;
		if($maxWidth) $this->maxWidth = $maxWidth;
		if($maxHeight) $this->maxHeight = $maxHeight;
		if($quality) $this->quality = $quality;
		
		list($width, $height) = getimagesize($this->path);
	
		if($width>$this->maxWidth) $checkWidth = false; else $checkWidth = true;
		if($height>$this->maxWidth) $checkHeight = false; else $checkHeight = true;
		
		if($checkWidth == false && $checkHeight == true){
			// adjust width
			$newWidth = $this->maxWidth;
			$newHeight = ($newWidth*$height)/$width;
		}
		else if($checkWidth == true && $checkHeight == false){
			// adjust height
			$newHeight = $this->maxWidth;
			$newWidth = ($width*$newHeight)/$height;
		}
		else if($checkWidth == false && $checkHeight == false){
			// adjust which is longer
			if($width>$height){	
				$newWidth = $this->maxWidth;
				$newHeight = ($newWidth*$height)/$width;
			}
			else {
				$newHeight = $this->maxWidth;
				$newWidth = ($width*$newHeight)/$height;
			}
		}
		else {
			$newWidth = $width;
			$newHeight = $height;
		}
		$image = imagecreatefromjpeg($this->path);
		$image_p = imagecreatetruecolor($newWidth, $newHeight);
		#$image_p = imagecreatetruecolor($this->maxWidth, $this->maxHeight);
		imagecopyresampled($image_p, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
		
		
		
		#$final = imagecreatetruecolor($this->maxWidth, $this->maxHeight);
		#imagecopyresampled($final, $image_p, 0, 0, 0, 0, $newWidth, $newHeight, $newWidth, $newHeight);
		
		// Output
		imagejpeg($image_p, $this->destination, $this->quality);
		#imagejpeg($final, $this->destination, $this->quality);
		
		imagedestroy($image_p);
	}
}
?>